package com.ruoyi.web.controller.wechat;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.domain.MsgReplyRule;
import com.ruoyi.wechat.service.MsgReplyRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpCurrentAutoReplyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


/**
 * 自动回复规则
 *
 * @author yan
 * @date 2021-11-08
 */
@Slf4j
@RestController
@RequestMapping("/manage/msgReplyRule")
@Api(tags = {"自动回复规则"})
public class MsgReplyRuleManageController {
    @Autowired
    private MsgReplyRuleService msgReplyRuleService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('wxmp:wxautoreply:index')")
    @ApiOperation(value = "列表")
    public AjaxResult list(Page page, MsgReplyRule msgReplyRule) {
        log.info("自动回复规则查询条件:{}", msgReplyRule);
        return AjaxResult.success("自动回复规则列表", msgReplyRuleService.selectReplyRuleList(page, msgReplyRule));
    }


    /**
     * 信息
     */
    @PreAuthorize("@ss.hasPermi('wxmp:wxautoreply:get')")
    @GetMapping("/info/{ruleId}")
    @ApiOperation(value = "详情")
    public AjaxResult info(@PathVariable("ruleId") Integer ruleId) {
        MsgReplyRule msgReplyRule = msgReplyRuleService.getById(ruleId);
        return AjaxResult.success("msgReplyRule", msgReplyRule);
    }

    /**
     * 保存
     */
    @PreAuthorize("@ss.hasPermi('wxmp:wxautoreply:add')")
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public AjaxResult save(@RequestBody MsgReplyRule msgReplyRule) {
        msgReplyRuleService.save(msgReplyRule);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('wxmp:wxautoreply:edit')")
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(@RequestBody MsgReplyRule msgReplyRule) {
        msgReplyRuleService.updateById(msgReplyRule);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('wxmp:wxautoreply:del')")
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@RequestBody Integer[] ruleIds) {
        msgReplyRuleService.removeByIds(Arrays.asList(ruleIds));

        return AjaxResult.success();
    }

    /**
     * 获取消息自动回复规则
     */
    @GetMapping("/Wx/getCurrentAutoreplyInfo")
    @ApiOperation(value = "获取消息自动回复规则")
    public AjaxResult getCurrentAutoreplyInfo(String appid) {
        try {
            WxMpCurrentAutoReplyInfo currentAutoReplyInfo = wxMpService.getCurrentAutoReplyInfo();
            log.info("获取消息自动回复规则:{}", currentAutoReplyInfo);
            return AjaxResult.success(currentAutoReplyInfo);
        } catch (WxErrorException e) {
            log.info("获取消息自动回复规则:", e);
            return AjaxResult.error("获取消息自动回复规则失败");
        }

    }

}
